import Vue from 'vue';
import message from './index.vue';

const MessageConstructor = Vue.extend(message);
let timer = null;
let instance;

const Message = function(options = {}) {
  if (Vue.prototype.$isServer) return;
  if (timer) return;
  instance = new MessageConstructor({
    data: options
  });
  document.body.appendChild(instance.$mount().$el);
  if (instance.message) {
    instance.visible = true;
  }
  timer = setTimeout(() => {
    instance.$destroy();
    instance.$el.parentNode.removeChild(instance.$el);
    timer = null;
  }, instance.time);
};

export default {
  install(vue) {
    vue.prototype.$message = Message;
  }
};
